#ifndef __GTC_DATA_H__
#define __GTC_DATA_H__

#include "rtc.h"
#include "stdbool.h"
#include "typedef.h"

//初始版本 1.1.1.0
//1.1.1.2 上报记录时过滤正在充电的报文
//1.1.1.3 文件写入后加fsync同步
//1.1.1.4 修改广告灯bug
//1.1.1.5 启动结束帧添加失败原因
//2.1.1.1 合并dc/ac为一个版本
//2.1.1.2 解决ac版本充电中不上传电压，电流数据bug

#define SOFT_VER "2.1.1.2"
#define SDEV_NUM_MAX 2 //枪数

#define CONFIG_ITEM_SIZE 20

#define FLASH_RECORD_HISTORY_MAX 250  //每个文件最大存储记录，两个文件循环存储，
#define FLASH_CONFIGUREFILE "configure.txt" //配置文件
#define FLASH_RECORDCHARGING "recordcharging.txt" //正在充电记录文件
#define FLASH_RECORDWAITUPLOAD "recordwaitupload.txt" //待上传记录文件
#define FLASH_RECORDHISTROYFILE "recordhistroy.txt" //历史记录文件
#define FLASH_RECORDHISTROYFILE2 "recordhistroy2.txt" //历史记录文件

#define FLASH_RECORDTMP "recordtmp.txt" //删除时的临时文件


#define FLASH_UPDATE_INFO_FILE "updateinfo.txt"   //升级文件下载信息记录
#define FLASH_UPDATE_MASTER_FILE "MasterAPP.bin"  //主机升级包

#define TRANSRECORDMAX 20
struct T_transRecord {
    uint8_t   recordState;
    uint32_t log_time;
    uint8_t   devId[TRANSRECORDMAX];
    uint32_t  subId;
    uint32_t beginTime;
    uint32_t endTime;
    char_t   dno[TRANSRECORDMAX]; 
    char_t   rno[TRANSRECORDMAX];
    char_t   userId[TRANSRECORDMAX];
    char_t   cardId[TRANSRECORDMAX];
    uint8_t   cType;
    uint32_t  cTypeValue;
    char_t  end_reason[TRANSRECORDMAX];
    unsigned short  acc_ah;
    unsigned short  acc_kwh;
    uint32_t  acc_second;
    uint32_t  av_power;
    uint32_t  meter_start;
    uint32_t  meter_end;
    uint8_t   res[50];
};

//运行参数，进程变化参数
typedef struct T_runTimeInfo {
    uint32_t powOnCnt;  //启动次数
    T_TimeFmt lastPowOn;  //上一次启动时间
    T_TimeFmt lastSignIn; //上一次签到时间
    T_TimeFmt lastCharge; //上一次充电时间
    uint8_t res[50];
}T_runTimeInfo; 

struct T_CostInfo {
    uint8_t type;
    uint32_t elecCost;
    uint32_t servCost;
};

enum{
    E_CHARGE_POWER_16A = 35,
    E_CHARGE_POWER_32A = 70,
};


int32_t data_getint(const char_t *key);
float data_getfloat(const char_t *key);
int32_t data_getstr(const char_t *key, char_t *str, int32_t size);
int32_t data_is_key_exist(const char_t *key);
int32_t data_setint(const char_t *key, uint32_t value, int32_t mode);
int32_t data_setfloat(const char_t *key, float value);
int32_t data_setstr(const char_t *key, const char_t *str);

void data_show_config(void);
void data_set_last_charge_time(T_TimeFmt *time);

uint32_t GtcData_GetSRandNum(void);
int32_t GtcData_GetSubDevId(int32_t pos);


struct T_transRecord;
void GtcData_AddRecordCharging(struct T_transRecord *transRecord);
void GtcData_UpdateRecordCharging(struct T_transRecord *transRecord);

void GtcData_TransRecordStateToWaitUpAll(void);
void GtcData_TransRecordStateToWaitUp(char_t *dno);
void GtcData_TransRecordStateToHistory(char_t *dno);
void GtcData_TransRecordStateToHistoryByBeginTime(char_t *time);

uint32_t GtcData_ReadRecordWaitSize(void);
int32_t GtcData_ReadRecordWaitPos(uint32_t pos, struct T_transRecord *record);

uint8_t GtcData_CheckUpdateFile(void);
int32_t GtcData_GetUpdateInfo(uint8_t *info, uint32_t len);
void GtcData_SaveUpdateInfoFile(uint8_t *info, uint32_t len);
int32_t GtcData_PushUpdateFileData(const char_t *name, int32_t pos, char_t *buf, int32_t len);

void GtcData_GetShareMem(char_t *buf, int32_t len);
void GtcData_SetShareMem(const char_t *buf, int32_t len);

void GtcData_SetCostSimple(const struct T_CostInfo *costInfo);
uint32_t GtcData_GetElecCost(void);
uint32_t GtcData_GetServCost(void);

void GtcData_Ls(void);
void GtcData_Rm(const char_t *name);
void GtcData_Cat(const char_t *name);
void GtcData_Format(void);

void GtcData_ConfigInfoPrint(void);
void GtcData_PrintfRecordWait(void);

void GtcData_Sync(void);
void GtcData_SaveToFile(void);
int32_t data_init(void);

void GtcData_SetSignInTim(uint32_t tim);
uint32_t GtcData_GetSignInTim(void);
void GtcData_SetLiveOutTim(uint8_t tim);
uint8_t GtcData_GetLiveOutTim(void);
void GtcData_SetLiveOutCnt(uint8_t cnt);
uint8_t GtcData_GetLiveOutCnt(void);
void GtcData_AddPowOnCnt(void);
uint32_t GtcData_GetPowOnCnt(void);
void GtcData_SetService(uint8_t state);
uint8_t GtcData_GetService(void);
void GtcData_SetCompanyName(uint8_t *para);
uint8_t *GtcData_GetCompanyName(void);
void GtcData_SetWebInfo(uint8_t *para);
uint8_t *GtcData_GetWebInfo(void);
void GtcData_SetPhone(uint8_t *para);
uint8_t *GtcData_GetPhone(void);
void GtcData_SetSettleInfo(uint8_t *para);

void GtcData_SetServiceInterface(uint8_t *para);

void GtcData_SetAttention(uint8_t *para);

void GtcData_SetQrcodeHeader(uint8_t *para);
uint8_t *GtcData_GetQrcodeHeader(void);
void GtcData_SetLastPowOn(T_TimeFmt *tim);
T_TimeFmt *GtcData_GetLastPowOn(void);
void GtcData_SetLastSignIn(T_TimeFmt *tim);
T_TimeFmt *GtcData_GetLastSignIn(void);
void GtcData_SetTransMode(uint8_t mode);
uint8_t GtcData_GetTransMode(void);
void GtcData_SetAutoUpTim(uint8_t sec);
uint8_t GtcData_GetAutoUpTim(void);

    
int32_t data_upgrade_config_file_set(char_t *url, char_t *md5);
int32_t data_get_sys_power_on_cnt(void);
void data_set_sys_power_on_cnt(void);
int32_t data_get_last_power_on_time_value(char_t *value);
int32_t data_get_last_power_on_time_str(char_t *str);
void data_set_last_power_on_time(T_TimeFmt *time);
int32_t data_get_last_charge_time(char_t *str);
int32_t data_get_last_charge_time_value(char_t *value);
void data_set_last_charge_time_str(T_TimeFmt *time);
void data_add_trace(char_t *msg);

enum E_EventType{
    EVENT_EVENT = 0,
    EVENT_ALARM_ON,
    EVENT_ALARM_OFF, 
};

enum E_EventCode{
    EVENT_SYSUP=0,
    EVENT_NETON,
    EVENT_NETOFF,
    EVENT_GUNIN,
    EVENT_GUNOFF,
    EVENT_START_CHARGE,
    EVENT_END_CHARGE,
    EVENT_UPDATE_SUCCESS,
    
};

void alarm_log(int32_t gunid, int32_t type, int32_t code, const char_t *desc);
void event_log(int32_t gunid, int32_t code);

int32_t data_save_update_info_file(char_t *info, uint32_t len);
int32_t dta_get_update_info(char_t *info, uint32_t len);
int32_t data_push_update_file_data(const char *name, int32_t pos, char_t *buf, uint32_t len);

#endif

